//
//  GameScene.m
//  MagicSword
//
//  Created by Jonear on 14/12/28.
//  Copyright (c) 2014年 Jonear. All rights reserved.
//

#import "MSMainGameScene.h"
#import "MSTopBarNode.h"
#import "MSHeroScene.h"
#import "MSChapterScene.h"
#import "MSTileMap.h"

@implementation MSMainGameScene

- (void)didMoveToView:(SKView *)view {
    /* Setup your scene here */
    [super didMoveToView:view];
    [self initSubViews];
}

- (void)initSubViews {
    float height = self.size.height - 42 - 60;
    float width = 320./364*height;
    float space = 25;
    float topSpace = 35;
    
    // 匹配模式
    SKSpriteNode *pvpModelNode = [SKSpriteNode spriteNodeWithImageNamed:@"main_pvpModel"];
    pvpModelNode.position = CGPointMake(width/2+space, topSpace+height/2);
    pvpModelNode.size = CGSizeMake(width, height);
    pvpModelNode.zPosition = 3;
    pvpModelNode.name = NodeName_pvpModelNode;
    [self addChild:pvpModelNode];
    
    // 第一章
    float modelWidth = 385./545*height;
    SKSpriteNode *model1Node = [SKSpriteNode spriteNodeWithImageNamed:@"main_Model1"];
    model1Node.position = CGPointMake(width+space*2+modelWidth/2, topSpace+height/2);
    model1Node.size = CGSizeMake(modelWidth, height);
    model1Node.zPosition = 3;
    model1Node.name = NodeName_Model1Node;
    [self addChild:model1Node];
}

- (void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
    
    // 匹配模式
    if ([[touchedNode name] isEqualToString:NodeName_pvpModelNode]) {
        [self runTouchDownAction:touchedNode];
    }
    // 第一章
    else if ([[touchedNode name] isEqualToString:NodeName_Model1Node]) {
        [self runTouchDownAction:touchedNode];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
    

    // 匹配模式
    if ([[touchedNode name] isEqualToString:NodeName_pvpModelNode]) {
        [ProgressHUD showError:@"功能开放中，尽请期待！"];
    }
    // 第一章
    else if ([[touchedNode name] isEqualToString:NodeName_Model1Node]) {
        MSChapterScene *chapterScene = [[MSChapterScene alloc] initWithSession:0];
        chapterScene.size = self.frame.size;
        chapterScene.scaleMode = SKSceneScaleModeAspectFill;
        [self.view pushScene:chapterScene];
    }
}

- (void)runTouchDownAction:(SKSpriteNode *)sprite {
    SKAction *action1_1 = [SKAction scaleTo:0.95 duration:0.2];
    SKAction *action1_2 = [SKAction fadeAlphaTo:0.8 duration:0.2];
    SKAction *action1 = [SKAction group:@[action1_1,action1_2]];
    
    SKAction *action2_1 = [SKAction scaleTo:1. duration:0.2];
    SKAction *action2_2 = [SKAction fadeAlphaTo:1. duration:0.2];
    SKAction *action2 = [SKAction group:@[action2_1,action2_2]];
    
    [sprite runAction:[SKAction sequence:@[action1, action2]]];
}

@end
